home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / COMMON / TOOLS / VB / CABINETS / MSDAO350.CAB / icontrols / MaskedEdit / FormatText.class (.txt) next >
Encoding:
Java Class File  |  1998-01-08  |  2.9 KB  |  104 lines

  1. package icontrols.MaskedEdit;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.DecimalFormat;
  5. import java.text.NumberFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8.  
  9. public class FormatText {
  10.    public static final int NUMERIC = 1;
  11.    public static final int DATETIME = 2;
  12.    public static final int STRING = 3;
  13.    private DecimalFormat decimalFormat;
  14.    private String format;
  15.    private int type;
  16.  
  17.    public String reformatText(String text) throws IllegalArgumentException {
  18.       String result = "";
  19.       if (this.type == 3) {
  20.          return text;
  21.       } else {
  22.          if (this.format.length() != 0 && text.length() != 0) {
  23.             if (this.type == 1) {
  24.                try {
  25.                   DecimalFormat df = this.decimalFormat;
  26.                   Double num = Double.valueOf(text);
  27.                   if (num == null) {
  28.                      throw new IllegalArgumentException("unable to parse text");
  29.                   }
  30.  
  31.                   result = ((NumberFormat)df).format(num);
  32.                } catch (Exception var7) {
  33.                   throw new IllegalArgumentException("unable to parse text");
  34.                }
  35.             } else {
  36.                for(int i = 0; i <= 3; ++i) {
  37.                   SimpleDateFormat sdf;
  38.                   switch (i) {
  39.                      case 0:
  40.                         sdf = (SimpleDateFormat)DateFormat.getDateInstance(3);
  41.                         break;
  42.                      case 1:
  43.                         sdf = (SimpleDateFormat)DateFormat.getTimeInstance(3);
  44.                         break;
  45.                      case 2:
  46.                         sdf = (SimpleDateFormat)DateFormat.getDateTimeInstance(3, 3);
  47.                         break;
  48.                      default:
  49.                         throw new IllegalArgumentException("unable to parse text");
  50.                   }
  51.  
  52.                   try {
  53.                      Date date = ((DateFormat)sdf).parse(text);
  54.                      if (date != null) {
  55.                         sdf = new SimpleDateFormat(this.format);
  56.                         result = ((DateFormat)sdf).format(date);
  57.                         break;
  58.                      }
  59.                   } catch (Exception var8) {
  60.                   }
  61.                }
  62.             }
  63.          }
  64.  
  65.          return new String(result);
  66.       }
  67.    }
  68.  
  69.    public String toString() {
  70.       return "{format=\"" + this.format + "\",type=" + this.type + "}";
  71.    }
  72.  
  73.    public FormatText() {
  74.       this("", 1);
  75.    }
  76.  
  77.    public FormatText(String format, int type) {
  78.       this.decimalFormat = null;
  79.       this.setFormat(format, type);
  80.    }
  81.  
  82.    public void setFormat(String format, int type) throws IllegalArgumentException {
  83.       if (type != 1 && type != 2 && type != 3) {
  84.          throw new IllegalArgumentException("invalid type");
  85.       } else {
  86.          this.format = format;
  87.          this.type = type;
  88.          this.decimalFormat = new DecimalFormat(format);
  89.       }
  90.    }
  91.  
  92.    public String getFormatString() {
  93.       return new String(this.format);
  94.    }
  95.  
  96.    public String reformatDouble(double value) throws IllegalArgumentException {
  97.       return this.decimalFormat.format(value);
  98.    }
  99.  
  100.    public int getFormatType() {
  101.       return this.type;
  102.    }
  103. }
  104.